package se.teamstat.client.gui.widgets.util;

import java.util.List;

import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.grid.ColumnConfig;

import se.teamstat.client.dto.EntityDTO;
import se.teamstat.client.dto.GridDTO;

public class Converter {

	public Object[][] convertListToArray(List<EntityDTO> entities) {
		Object[][] objects = null;
		int counter = 0;
		for(EntityDTO entity:entities) {
			String entityText = entity.toString();
			Object[] entityObject = entityText.split(",");
			if (objects==null) {
				objects = new Object[entities.size()][entityObject.length];
			}
			for(int i=0;i<entityObject.length;i++) {
				objects[counter][i] = entityObject[i];
			}
			counter++;
		}
		return objects;
	}
	
	public RecordDef convertListToRecordDef(List<GridDTO> gridDTOs) {
		RecordDef recordDef = null;
		int counter = 0;
		FieldDef[] fieldDef = new FieldDef[gridDTOs.size()];
		for(GridDTO gridDTO:gridDTOs) {
			String dataIndex = gridDTO.getDataIndex();
			StringFieldDef stringFieldDef = new StringFieldDef(dataIndex);
			fieldDef[counter] = stringFieldDef;	
			counter++;
		}
		recordDef = new RecordDef(fieldDef);
		return recordDef;
	}
	
	public ColumnConfig[] convertListToColumnConfig(List<GridDTO> gridDTOs) {
		ColumnConfig[] columnConfigs = new ColumnConfig[gridDTOs.size()];
		int counter = 0;
		for(GridDTO gridDTO:gridDTOs) {
			String header = gridDTO.getHeader();
			String dataIndex = gridDTO.getDataIndex();
			String id = gridDTO.getId();
			int width = gridDTO.getWidth();
			boolean sortable = gridDTO.isSortable();
			
			ColumnConfig columnConfig = new ColumnConfig(header, dataIndex, width, sortable, null, id);
			columnConfigs[counter] = columnConfig;
			counter++;
		}
		return columnConfigs;
	}
}
